using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Game;
using UnityEngine;

namespace IQIGame.Onigao.GamePlay
{
    public class UIEquipListScreen : UIBaseWindow
    {
        /// <summary>
        /// 静态配置
        /// </summary>
        private static UICreateConfig _createConfig = new()
        {
            prefabName = nameof(UIEquipListScreen),
            layer = EUILayer.Normal,
        };

        /// <summary>
        /// 创建UI的配置
        /// </summary>
        public override IUICreateConfig createConfig => _createConfig;

        #region Template Generate,don't modify
        protected partial class UIB_UIEquipListScreen
        {
            #region ObjectBinding Generate 
            public IQIGame.Onigao.Framework.ExButton btnConfirm { protected set; get; }
            public IQIGame.Onigao.Framework.ExButton btnCancel { protected set; get; }
            public GameObject toggleSubType { protected set; get; }
            public GameObject toggleLocation { protected set; get; }
            public virtual void InitBinding(ObjectBinding __binding)
            {
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExButton>("btnConfirm", out var __tbv0);
                this.btnConfirm = __tbv0;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExButton>("btnCancel", out var __tbv1);
                this.btnCancel = __tbv1;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("toggleSubType", out var __tbv2);
                this.toggleSubType = __tbv2;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("toggleLocation", out var __tbv3);
                this.toggleLocation = __tbv3;
            }
            #endregion ObjectBinding Generate 
        }
        #endregion Template Generate,don't modify

        #region fields & properties
        ListScrollAdapter<UIN_CommonToggleOn> subTypes;
        ListScrollAdapter<UIN_CommonToggleOn> locations;

        EquipFilter currFitler = EquipFilter.Default;

        protected UIB_UIEquipListScreen ui { get; set; }
        #endregion

        protected override void BeforeInit()
        {
            ui = new UIB_UIEquipListScreen();
            ui.InitBinding(this.csObjBind);
        }

        protected override void OnInit()
        {
            this.subTypes = this.InitListScroll<UIN_CommonToggleOn>(this.ui.toggleSubType, this.OnSubTypeChanged);
            this.locations = this.InitListScroll<UIN_CommonToggleOn>(this.ui.toggleLocation, this.OnLocationChanged);

            this.ui.btnConfirm.onClick.AddListener(this.OnClickConfirm);
            this.ui.btnCancel.onClick.AddListener(this.OnClickCancel);
        }

        protected override void OnShow()
        {

        }

        protected override void OnHide()
        {

        }

        void OnClickConfirm()
        {
            MsgDispatcher.Broadcast(MsgEventType.EquipFilter, this.currFitler);
            this.Close();
        }

        void OnClickCancel()
        {
            this.Close();
        }

        protected override void OnDispose()
        {
            this.subTypes = null;
            this.locations = null;
        }

        protected override void OnShow(UIBaseData data)
        {
            if (data is UD_EquipListCreen d)
            {
                this.currFitler.Sync(d.filter);
            }
            this.subTypes.RefillCells(APIUIEquip.AllQualitys.Length);
            this.locations.RefillCells((int)UIEquipLocationType.MaxLength - 1);
        }

        void OnSubTypeChanged(UIN_CommonToggleOn item, int index)
        {
            string lab = APIUIEquip.AllQualitys[index];
            bool isOn = this.currFitler.quality == (APIUIEquip.QualityStartValue + index);
            item.SetDefaultIsOn(isOn);
            item.SetCallOnValChanged(this.OnSubTypeSelected);
            item.Show(index, lab);
        }

        void OnSubTypeSelected(int index, bool isOn)
        {
            this.currFitler.quality = isOn ? APIUIEquip.QualityStartValue + index : 0;
        }

        void OnLocationChanged(UIN_CommonToggleOn item, int index)
        {
            int nType = index + 1;
            string lab = APIUIEquip.GetLocationName(nType);
            bool isOn = this.currFitler.locationType == nType;
            item.SetDefaultIsOn(isOn);
            item.SetCallOnValChanged(this.OnLocationSelected);
            item.Show(index, lab);
        }

        void OnLocationSelected(int index, bool isOn)
        {
            this.currFitler.locationType = isOn ? index + 1 : 0;
        }
    }
}
